package com.gdssoft.pdaweb.dao.am.impl;
/*
 *  捷达世软件（深圳）有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gdssoft.core.Constants;
import com.gdssoft.core.dao.impl.BaseDaoImpl;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.pdaweb.dao.am.PdaAmMeterDao;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;

public class PdaAmMeterDaoImpl extends BaseDaoImpl<PdaAmMeter> implements PdaAmMeterDao{

	public PdaAmMeterDaoImpl() {
		super(PdaAmMeter.class);
	}

	@Override
	public PdaAmMeter getByAmId(String amId) {
		List<PdaAmMeter> list = new ArrayList<PdaAmMeter>();
		try{
			list = findByHql("from PdaAmMeter pm where pm.amId='"+amId+"'");
		}catch(Exception e){
			logger.debug("PdaAmMeterList query error:"+e.getMessage());			
		}
		return list.size()>0?list.get(0):new PdaAmMeter();
	}

	@Override
	public List<PdaAmMeter> getAllForExcel() {
		return findByHql("from PdaAmMeter p where p.type=2");
	}
	
	@Override
	public List<PdaAmMeter> getAmExcel(String idS) {
		return findByHql("from PdaAmMeter p where p.type=2 and p.department.depId in ("+idS+") ");
	}

	@Override
	public List<PdaAmMeter> getByPosition(String districtId, String buildingId) {
		List<PdaAmMeter> list = new ArrayList<PdaAmMeter>();
		try{
			String hql = "from PdaAmMeter pm where pm.isPlanned=? and pm.districtId=?";
			
			hql += buildingId.isEmpty()?"":" and pm.buildingId='"+buildingId+"'";
			
			list = findByHql(hql,new Object[]{Constants.UNPLANNED,districtId});
		}catch(Exception e){
			logger.debug("PdaAmMeter query error:"+e.getMessage());			
		}
		return list;
	}
	@Override
	public List<PdaAmMeter> getByHouseId(String houseId){
		String hql = "from PdaAmMeter pam where pam.aparmentId=?";
		return this.findByHql(hql,new String[]{houseId});
	}
	@Override
	public PdaAmMeter getByBarcode(String barcode){
		String hql = "from PdaAmMeter pam where pam.pdaMdAssetInfo.barCode=?";
		return (PdaAmMeter)findUnique(hql,new String[]{barcode});
	}

	@Override
	public boolean isCrmMeterNoExist(String crmMeterNo) {
		//return findByHql("from PdaAmMeter p where p.crmMeterNo=?",new Object[]{crmMeterNo}).size()>0;
		return false;
	}

	@Override
	public boolean pauseAndResume(String recId, String vType, AppUser appUser,String dateT) {
		// TODO Auto-generated method stub
		String sql ="UPDATE PDA_AM_METER\n" 
		           +"    SET TYPE = ?, LASTEDITBY = ?, LASTEDITDT = ?\n"  
				   +"  WHERE AM_ID = ?";
		String[] param= new String[]{vType,appUser.getUsername(),dateT,recId};
		int count = jdbcTemplate.update(sql,param);
		if(count>0){
			return true;
		}
		return false;
	}

}